<?php
/**
 * File keep Llib_List class
 *
 * @author Yaroslav Olekh
 */


/**
 * Llib_List class
 *
 */
class Llib_List
{
    protected $data = array();

    protected $types = array(
    	'checkbox' => array(
    	                   'img' => array('on' => 'active.png', 'off' => 'active_no.png')
    					  ),
    	'order' => array(
    	                   'img' => array('up' => 'arrow_up.png', 'down' => 'arrow_down.png')
    					  ),
    	'button' => array(
    	                   'img' => 'delete.png'
    					  )
    );

    public $func = array();
    public $fields = array();

    public $page;
    public $sort;
    public $dr;
    public $s;
    public $pager_count;
    public $table_name;

	function __construct($request, $sess, $table_name = null)
	{
    	if(!isset($sess->pager_count)) $sess->pager_count = 50;

    	$this->table_name = is_null($table_name) ? $request->getControllerName() : $table_name;
    	$this->page = $request->getParam('page', 1);
    	$this->sort = $request->getParam('sort', null);
    	$this->dr = $request->getParam('dr', null);
    	$this->pager_count = $request->getParam('pager_count', null);

    	if(is_null($this->pager_count)) $this->pager_count = $sess->pager_count;
    	else $sess->pager_count = $this->pager_count;

    	if(is_null($this->sort)) $this->sort = $sess->sort;
    	else $sess->sort = $this->sort;

		if(is_null($this->dr)) $this->dr = $sess->dr;
    	else $sess->dr = $this->dr;

    	if(!is_null($this->sort))
    	   $this->s = $this->sort.($this->dr ? ' DESC' : ' ASC');

    	$this->AddSort();
    	$this->AddCountList($this->pager_count);
	}


    /*public function AddParams($params = array())
    {
    	$this->data['params'][] = $params;
    }*/




    //$title, /*  */
    	//$name, /* Field Name */
    	//$att = array())

/**
 *  function AddColumn
 *  1) string title
 *  2) string field_name
 * 	3) string sort_field_name
 * 	4) array attributes
 */

    public function AddColumn()
    {
    	$args = func_get_args();

    	if(is_string($args[0])) $clmn['title'] = $args[0];
    	if(is_string($args[1])) $clmn['field_name'] = $args[1];

    	if(is_string($args[2])) $clmn['sort_field_name'] = $args[2];
    	else $clmn['sort_field_name'] = $args[1];

    	if(isset($args[2]) && is_array($args[2])) $att = $args[2];
    	if(isset($args[3]) && is_array($args[3])) $att = $args[3];

    	if(count($att) > 0)
    	foreach($att as $k => $v) $clmn['__attributes'][$k] = $v;

    	$this->data['head'][] = $clmn;

    	$this->fields[] = $clmn['field_name'];
    }


    public function AddFunc($params = array(
    	'name' => 'active',
    	'type' => 'checkbox',
    	'field' => 'is_active',
    	'title' => 'Активний',
    	'alt' => 'Активний',
    	'alt_off' => 'Не активний',
    	'url' => null
    	))
    {

    	if(!isset($params['name']) || $params['name'] == '') $params['name'] = 'func'.time();
    	if(!isset($params['type']) || $params['type'] == '') $params['type'] = 'checkbox';
    	if($params['type'] == 'order') {$params['field'] = 'norder'; $params['title'] = 'Порядок';$params['name'] = 'order';}
    	if(!isset($params['title']) || $params['title'] == '') $params['title'] = '';


    	switch($params['type'])
    	{
    		case 'button':
    			$img = array('img' => (isset($params['img']) ? $params['img'] : $params['name'].'.png'));
    			break;
    		default:
    		    if(!isset($params['field']) || $params['field'] == '') return false;
    			$img = $this->types[$params['type']];
    	}

    	$this->func[] =	array_merge($params, $img);
    	$this->data['func'] = $this->func;
    	$this->data['func']['__attributes']['count'] = count($this->func);
    }


    public function AddParams($data = array())
    {
    	$this->data['params'] = $data;
    }

    public function AddData($data = array())
    {
    	$this->data['data'] = $data;
    }

    public function GetList()
    {

    	$this->data['head']['__attributes']['count'] = count($this->data['head']) + (isset($this->data['func']) ? 1 : 0);
    	$this->data['head']['__attributes']['table_name'] = $this->table_name;

    	foreach($this->data['data']['data'] as $k => $item)
    	{
    		foreach($this->fields as $field)
    		{
    			$this->data['data']['data'][$k][$field] = array('__attributes' => array('type' => 'field'), $item[$field]);
    		}

    		foreach($this->func as $j => $f)
    		{
    			if(isset($f['field'])) 	$f['value'] = $item[$f['field']];
    			//$this->data['data']['data'][$k][$f['name']] = array('__attributes' => array('type' => 'func'), $f);
    			$this->data['data']['data'][$k][$f['name']] = $f;
    			$this->data['data']['data'][$k][$f['name']]['__attributes'] = array('type' => 'func');
    		}
    	}

    	return $this->data;
    }

    public function AddSort()
    {
    	$this->data['head']['__attributes']['sort'] = $this->sort;
    	$this->data['head']['__attributes']['dr'] = $this->dr;
    }

    public function AddCountList($a)
    {
		for($i = 10, $k = 0; $i <= 50; $i += 10, $k++)
		{
    		$this->data['pager_count_list'][$k] = $i;
		}

		$this->data['pager_count_list']['__attributes']['active'] = $a;
    }
}
